﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Entity;
using PBOffice.DAL;
using PBOffice.BLL;
public partial class department : System.Web.UI.Page
{
    Department dep = new Department();
    v_UserDepPosition vdep = new v_UserDepPosition();
    DepartmentManager depManager = new DepartmentManager();
    OrganManager organManager = new OrganManager();
    TreeViewManager tvManager = new TreeViewManager();
    public void Page_Load(object sender, EventArgs e)
    {
        //部门管理页面首次加载
        if(!IsPostBack)
        {
            tvManager.tv_Load(tv);       //加载机构树信息
            btnChange.Visible = false;       //修改按钮不显示
        }
        
    }
    /// <summary>
    /// 选中节点变化事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void tv_SelectedNodeChanged(object sender, EventArgs e)
    {
        txtDepId.Text = "";
        txtDepCode.Text = "";
        txtDepDescribe.Text = "";
        rbtnlStatus.SelectedIndex = 0;
        tvManager.tv_SelectedNodeChanged(tv);      //展开当前选中节点 
        if (tv.SelectedNode.ChildNodes.Count > 0)    //选中节点有子结点
        {
            //tvManager.tv_SelectedNodeChanged(tv);      //展开当前选中节点
            btnChange.Visible = false;       //修改按钮隐藏
        }
        else                                  //选中节点没有子结点
        {
            if (tv.SelectedNode.Parent != null)
            {
                btnChange.Visible = true;       //修改按钮显示
                int depNo = int.Parse(tv.SelectedValue.ToString());     //部门编号
                v_DepOrgan depSelected = depManager.ShowDepInfoByDepNo(depNo);  //根据部门编号查询部门信息
                //显示选中部门节点的信息
                txtDepId.Text = depSelected.DepId;
                txtDepCode.Text = depSelected.DepCode;
                txtDepDescribe.Text = depSelected.DepDescribe;
                if (depSelected.DepStatus == true)
                    rbtnlStatus.SelectedIndex = 0;     //状态启用
                else
                    rbtnlStatus.SelectedIndex = 1;     //状态禁用          
            }
            else
            {
                btnChange.Visible = false;       //修改按钮不显示
            }
        }
    }
    /// <summary>
    /// 添加按钮单击事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            int  organNo; //机构编号
            int depNo = Convert.ToInt32(tv.SelectedValue.ToString());     //选中节点的部门编号
            if (tv.SelectedNode.Parent != null)         //非根节点
            {
                v_DepOrgan d = depManager.ShowDepInfoByDepNo(depNo);     //根据部门编号查询部门信息 
                organNo = d.OrganNo;
                dep.DepParentNo = depNo;      //部门父节点编号
            }
            else                                        //跟结点时
            {
                organNo = Convert.ToInt32(tv.SelectedValue.ToString()); //所属机构编号
            }
            NewDepInfo();    //新的部门信息           
            bool result = depManager.AddDepInfo(dep,organNo);     //添加部门信息
            if (result == true)
            {
                Page.ClientScript.RegisterClientScriptBlock(typeof(string), "OK", "alert('添加成功！')", true);
                TreeNode tn = new TreeNode();
                tn.Text = dep.DepId;
                tn.Value = dep.DepNo.ToString();
                tv.SelectedNode.ChildNodes.Add(tn);    //将新的部门信息添加到视图树上
                txtDepId.Text = "";
                txtDepCode.Text = "";
                txtDepDescribe.Text = "";
                rbtnlStatus.SelectedIndex = 0;
            }
            else
                Page.ClientScript.RegisterClientScriptBlock(typeof(string), "Error", "alert('添加失败！')", true); 

        }
    }
     /// <summary>
     /// 获取新的部门信息
     /// </summary>
    private void NewDepInfo()
    {
       
        dep.DepId = txtDepId.Text.Trim();           //部门名称
        dep.DepCode = txtDepCode.Text.Trim();       //部门代码
        dep.DepDescribe = txtDepDescribe.Text.Trim();//部们描述
        if (rbtnlStatus.SelectedIndex == 0)     //部门状态选中第一项
            dep.DepStatus = true;     //部门状态：启用
        else                                    //部门状态选中第二项
            dep.DepStatus = false;    //部门状态：禁用              
    }
    /// <summary>
    /// 修改按钮单击事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnChange_Click(object sender, EventArgs e)
    {
        if(Page.IsValid)
        {
            dep.DepNo = Convert.ToInt32(tv.SelectedValue.ToString());  //部门编号
            v_DepOrgan depNew = depManager.ShowDepInfoByDepNo(dep.DepNo);   //根据部门编号查询部门信息
            dep.DepNo = depNew.DepNo;
            int organNo= depNew.OrganNo;
            NewDepInfo();     //新的部门信息
            bool result = depManager.ChangeDepInfo(dep,organNo);    //修改部门信息
            if (result == true)
            {
                Page.ClientScript.RegisterClientScriptBlock(typeof(string), "OK", "alert('修改成功！')", true);
                tv.SelectedNode.Text = txtDepId.Text;
                txtDepId.Text = "";
                txtDepCode.Text = "";
                txtDepDescribe.Text = "";
                rbtnlStatus.SelectedIndex = 0;
            }
            else
                Page.ClientScript.RegisterClientScriptBlock(typeof(string), "Error", "alert('修改失败！')", true); 
        }
    }
}